using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 抓取Buff - 将目标拉向施法者并造成伤害
/// </summary>
public class GrappleBuff : BuffBase
{
    private Vector3 m_GrappleDirection;
    private float m_GrappleSpeed;
    private float m_GrappleDistance;
    private bool m_IsGrappling;
    private long m_GrappleDamage;

    public GrappleBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is GrappleData grappleData)
        {
            m_GrappleSpeed = grappleData.GrappleSpeed;
            m_GrappleDistance = grappleData.GrappleDistance;
            m_GrappleDamage = grappleData.GrappleDamage;
        }
        else
        {
            m_GrappleSpeed = 10.0f; // 默认抓取速度
            m_GrappleDistance = 1.0f; // 默认抓取距离
            m_GrappleDamage = 20; // 默认抓取伤害
        }
        
        // 计算抓取方向（朝向施法者）
        if (Caster != null)
        {
            m_GrappleDirection = (Caster.transform.position - Target.transform.position).normalized;
        }
        else
        {
            m_GrappleDirection = Vector3.left; // 默认向左抓取
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        m_IsGrappling = true;
        
        // 播放抓取特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("grappled", true);
        }
        
        Logger.Log($"{Target.name} ���{Caster.name} 抓取");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        if (m_IsGrappling && Caster != null)
        {
            // 计算目标位置（施法者附近）
            Vector3 targetPos = Caster.transform.position - m_GrappleDirection * m_GrappleDistance;
            
            // 执行抓取移动
            Vector3 currentPos = Target.transform.position;
            Vector3 newPos = Vector3.MoveTowards(currentPos, targetPos, m_GrappleSpeed * dt);
            Target.transform.position = newPos;
            
            // 检查是否到达目标位���
            if (Vector3.Distance(newPos, targetPos) < 0.5f)
            {
                // 到达目标位置，造成伤害
                Target.HealthComponent.TakeDamage(m_GrappleDamage);
                Logger.Log($"{Target.name} 受到抓取伤害: {m_GrappleDamage}");
                
                // 触发伤害事件
                var args = ReferencePool.Acquire<EntityTakeDamageEventArgs>();
                args.Entity = Target;
                args.Damage = m_GrappleDamage;
                args.Attacker = Caster;
                EventHelper.Fire(this, args);
                
                m_IsGrappling = false;
            }
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        m_IsGrappling = false;
        Logger.Log($"{Target.name} 抓取效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        m_IsGrappling = false;
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("grappled", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Grapple"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 抓取效果不能叠加，只能刷新持续时���
    }
}
